package com.jzy.bxb.app.scripts.http.order;

import com.jzy.bxb.app.server.AuthHttpHandler;
import com.jzy.bxb.core.handler.HandlerEntity;
import com.jzy.bxb.model.constant.OrderState;
import com.jzy.bxb.model.mongo.dao.OrderDao;
import com.jzy.bxb.model.mongo.dao.UserDao;
import com.jzy.bxb.model.mongo.entity.Order;
import com.jzy.bxb.model.mongo.entity.User;
import com.jzy.bxb.model.util.UMPushUtil;

/**
 * 改变订单状态
 * <p>
 * http://127.0.0.1:8680/order/change.state?orderId=0&state=1&sid=b6c15c14-c159-
 * 40b8-a292-93eb1fcfdd29 orderId 订单ID <br>
 * state 状态 4订单完成
 * 
 * </p>
 *
 * @author JiangZhiYong
 * @date 2017-07-13 QQ:359135103
 */
@HandlerEntity(path = "/order/change.state")
public class ChangeOrderStateHandler extends AuthHttpHandler {

	@Override
	public void run() {
		long orderId = getLong("orderId");
		Order order = OrderDao.getById(orderId);
		if (order == null) {
			sendError("订单不存在");
			return;
		}
		int state = getInt("state");
		if (order.getState() >= state) {
			sendError("订单状态异常");
			return;
		}
		order.setState(state);
		if (OrderState.PAY_FINISH.getState() == state) {
			User employer = UserDao.getById(order.getEmployerId());
			if (employer != null) {
				if(employer.isIOS()){
					UMPushUtil.sendIOSCustomizedcast("订单完成", null, employer.getUserName());
				}else{
					UMPushUtil.sendAndroidCustomizedcast("订单完成", "订单完成",
							"你的订单已被处理", null, employer.getUserName());
				}
				
			}
		}

		OrderDao.saveOrder(order);
		sendMsg(order);

	}

}
